﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Toenda.Foundation.Net {
	/// <summary>
	/// Implements a validator and converter for web related data
	/// </summary>
	public class WebValidator {
		/// <summary>
		/// Checks if a text is a webaddress
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		public static bool IsWebAddress(string text) {
			if(text.StartsWith("http://")
			|| text.StartsWith("https://")
			|| text.StartsWith("ftp://")
			|| text.StartsWith("ftps://")) {
				return true;
			}
			else {
				return false;
			}
		}

		/// <summary>
		/// Check an email adress for its correctness
		/// </summary>
		/// <param name="text">A email adress to check.</param>
		/// <exception cref="ArgumentNullException">If inputEmail is null.</exception>
		/// <returns></returns>
		public static bool IsEmail(string text) {
			if(text == null) {
				throw new ArgumentNullException("text");
			}

			string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}"
				+ @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\"
				+ @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

			Regex pattern = new Regex(strRegex);

			if(pattern.IsMatch(text)) {
				return true;
			}
			else {
				return false;
			}
		}

		/// <summary>
		/// Cleanup all linebreaks
		/// </summary>
		/// <param name="text">The text.</param>
		/// <returns></returns>
		public static string CleanupLinebreak(string text) {
			return text.Replace("\r\n", " ");
		}

		/// <summary>
		/// Convert a new line char into a html-[br]
		/// </summary>
		/// <param name="text">The text.</param>
		/// <returns></returns>
		public static string ConvertLinebreakToHtml(string text) {
			return text.Replace("\r\n", "<br />");
		}

		/// <summary>
		/// Convert a new line char into a html-[br]
		/// </summary>
		/// <param name="text">The text.</param>
		/// <returns></returns>
		public static string ConvertLinebreak(string text) {
			return text.Replace("\r\n", "<br />");
		}

		/// <summary>
		/// Convert a html-[br] into a new line char
		/// </summary>
		/// <param name="text">The text.</param>
		/// <returns></returns>
		public static string ConvertHtmlToLinebreak(string text) {
			text = text.Replace("<br />", "\r\n");
			text = text.Replace("<br/>", "\r\n");
			text = text.Replace("<br>", "\r\n");
			return text;
		}

		/// <summary>
		/// Convert a html-[br] into a new line char
		/// </summary>
		/// <param name="text">The text.</param>
		/// <returns></returns>
		public static string ConvertHtmlLinebreak(string text) {
			text = text.Replace("<br />", "\r\n");
			text = text.Replace("<br/>", "\r\n");
			text = text.Replace("<br>", "\r\n");
			return text;
		}

		/// <summary>
		/// Correct a web adress
		/// </summary>
		/// <param name="webAdress">A web url adress.</param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException">If WebAdress is null.</exception>
		public static string CorrectWebAdress(string webAdress) {
			if(webAdress == null) {
				throw new ArgumentNullException("WebAdress");
			}

			if(webAdress.Substring(0, 7) == "http://"
			|| webAdress.Substring(0, 8) == "https://"
			|| webAdress.Substring(0, 6) == "ftp://"
			|| webAdress.Substring(0, 7) == "ftps://"
			|| webAdress.Substring(0, 7) == "mailto:") {
				return webAdress;
			}
			else {
				return "http://" + webAdress;
			}
		}

		/// <summary>
		/// Correct a web adress to a given type
		/// </summary>
		/// <param name="webAdress">A web url adress.</param>
		/// <param name="adressPrefix">The prefix to cerrect.</param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException">If WebAdress is null.</exception>
		public static string CorrectWebAdress(string webAdress, WebAdressPrefix adressPrefix) {
			if(webAdress == null) {
				throw new ArgumentNullException("WebAdress");
			}

			if(webAdress.Substring(0, 7) == "http://"
			|| webAdress.Substring(0, 6) == "ftp://"
			|| webAdress.Substring(0, 8) == "https://") {
				return webAdress;
			}
			else {
				switch(adressPrefix) {
					case WebAdressPrefix.Undefined:
						return "http://" + webAdress;
					case WebAdressPrefix.HTTP:
						return "http://" + webAdress;
					case WebAdressPrefix.HTTPS:
						return "https://" + webAdress;
					case WebAdressPrefix.FTP:
						return "ftp://" + webAdress;
					case WebAdressPrefix.FTPS:
						return "ftps://" + webAdress;
					case WebAdressPrefix.EMAIL:
						return "mailto:" + webAdress;
					default:
						return "http://" + webAdress;
				}
			}
		}

		/// <summary>
		/// Clear a email string from some special chars and spaces
		/// </summary>
		/// <param name="text">The mail.</param>
		/// <returns></returns>
		public static string CleanEmailString(string text) {
			string replaceChars = "|~+*#'^°?´`$%&³²{[]}§=€();:,<>!/";

			foreach(char c in replaceChars) {
				text = text.Replace(c, ' ');
			}

			return text.Replace(" ", "").Trim();
		}
	}
}
